Basic operations

In [1]:
1.7
Out[1]:
1.7
In [35]:
#comments are written like this these lines after '#' are not executed
In [2]:
type(1)
Out[2]:
int
In [3]:
type(2.3)
Out[3]:
float
In [4]:
type("hello") #quotes important 
Out[4]:
str
In [18]:
type(True)
Out[18]:
bool
In [6]:
float(1)
Out[6]:
1.0
In [7]:
int(2.7)
Out[7]:
2
In [8]:
3+2
Out[8]:
5
In [12]:
3.0+2 #answer float
Out[12]:
5.0
In [9]:
print(3+5)
8
In [10]:
5-7
Out[10]:
-2
In [11]:
7*8
Out[11]:
56
In [13]:
5/2 #float answer
Out[13]:
2.5
In [14]:
5//2 #integer answer
Out[14]:
2
In [15]:
5%2 # reminder
Out[15]:
1
In [16]:
5**2 #power
Out[16]:
25

operations prefernce

- parentheses ()
- power **
- multiplication *
- division /
- addition or substraction + or -
- executed from left to right 
In [23]:
--4
Out[23]:
4
In [24]:
-4
Out[24]:
-4
In [26]:
++4
Out[26]:
4
In [27]:
+4
Out[27]:
4

Variables

In [34]:
pi= 3.14 # varibales store values here pi is a variable 
r = 3 # always value on the right is assigned to left like in math a = 1 and 1 = a  second will not work in programing 
area = pi * r**2
area
Out[34]:
28.26
In [30]:
area = pi * (r**2)
area
Out[30]:
28.26
In [33]:
area = (pi * r)**2
area #wrong way but to show the impotance of brackets 
Out[33]:
88.7364
In [42]:
x = 1
x
Out[42]:
1
In [43]:
x = 1
x= x+1
x
Out[43]:
2
In [45]:
x = 1
x += 1 # same as x=x+1 called shorthand operator 
x
Out[45]:
2

Operators and Branching

In [46]:
2>3.0
Out[46]:
False
In [48]:
5/5 == 1 #check if equal too remember double ==
Out[48]:
True
In [49]:
3>3
Out[49]:
False
In [50]:
3>=3
Out[50]:
True
In [52]:
2<2
Out[52]:
False
In [53]:
2<=2
Out[53]:
True
In [55]:
2!=2 #not equal too
Out[55]:
False
In [56]:
2!=3
Out[56]:
True
In [57]:
2==2 and 2+2 ==4 #and gives true when both conditions are TRUE
Out[57]:
True
In [58]:
2==2 and 2+2 == 5 #Left one false 
Out[58]:
False
In [59]:
2==3 and 2+2 == 5 #both false
Out[59]:
False
In [60]:
2==2 or 2+2 ==4 #or gives when at least one is TRUE
Out[60]:
True
In [62]:
2==2 or 2+2 == 5 #Left expression false but right expression is true 
Out[62]:
True
In [63]:
2==3 or 2+2 == 5 #both false
Out[63]:
False
In [65]:
x = 4
if x%2 ==0: # statement should end with a ':'
    print("even") #indentation is imporant hence use tab after if statement 
else:
    print("odd")
even
In [66]:
#same code as before with x assigned to odd
x = 7
if x%2 ==0: # statement should end with a ':'
    print("even")
else:
    print("odd")
odd
In [69]:
x = 0 # just an another one
if x%2 ==0: # statement should end with a ':'
    print("even")
else:
    print("odd")
even
In [75]:
#Nested if conditions and notice the indendations 
x = 9
if x%2 ==0:
    if x%3 ==0:
        print("Divisible by both 2 and 3")
    else:
        print("Divisible by 2 but not 3")
else:
    if x%3==0:
        print("Not divisible by 2 but divisible by 3")
    else:
        print("Not divisible by both 2 and 3")
Not divisible by 2 but divisible by 3
In [76]:
#one example of same code 
x = 6
if x%2 ==0:
    if x%3 ==0:
        print("Divisible by both 2 and 3")
    else:
        print("Divisible by 2 but not 3")
else:
    if x%3==0:
        print("Not divisible by 2 but divisible by 3")
    else:
        print("Not divisible by both 2 and 3")
Divisible by both 2 and 3
In [5]:
# use of conditions 
x=2
y=3
z=5

if x<y and x<z:
    print("x is smallest")
elif y<z and y<x:           #notice the elif same as else if 
    print("y is smallest")
else:
    print("z is smallest")  # and logic failes when the values if x,y and z are equal 
                            # can you guess the output the answer when x=y=z
x is smallest
In [8]:
x=float(input("x=")) #takes user input 
y=float(input("y="))
z=float(input("z="))

if x<y and x<z:
    print("x is smallest")
elif y<z and y<x:
    print("y is smallest")
else:
    print("z is smallest")
x=7
y=8
z=15
x is smallest
In [16]:
3 > 4 or 4 < 5 and 10 < 9 #conditions execuited left to right 
Out[16]:
False
In [17]:
#keywords can't be used as varibales 

Swaping values in variables

In [18]:
x=2
y=3
temp = x #using  extra 'temp' varibale 
x = y
y = temp
print("x=",x)
print("y=",y)
x= 3
y= 2

Strings

In [19]:
h = "hello" #double or single quotes will work
h
Out[19]:
'hello'
In [20]:
'hello' + 'there'
Out[20]:
'hellothere'
In [22]:
'hello' +' '+ 'there!'
Out[22]:
'hello there!'
In [26]:
s = "Hello"
s*3
Out[26]:
'HelloHelloHello'
In [28]:
s = "Hello " #space after o 
s*3
Out[28]:
'Hello Hello Hello '
In [29]:
s[0]
Out[29]:
'H'
In [30]:
s[4]
Out[30]:
'o'
In [31]:
'MIT'[1]
Out[31]:
'I'
In [33]:
'hello'[1:3] # 2nd and 3rd element 
Out[33]:
'el'
In [34]:
'hello'[1:]  # everything from 1st element 
Out[34]:
'ello'
In [36]:
'hello'[:3] #everything upto 3rd element 
Out[36]:
'hel'
In [37]:
'hello'[:]
Out[37]:
'hello'
In [38]:
'hello'[:-1] #everything but last
Out[38]:
'hell'
In [39]:
'hello'[-3:] #last 3 elements 
Out[39]:
'llo'
In [71]:
"hello"[1:5:2] #every second element 
Out[71]:
'el'
In [112]:
"hello"[:2]
Out[112]:
'he'
In [115]:
"hello"[:2:-1] # -step gives elements not slected and the reverse jumping 
# selected last 2 elements and goes in opposite direction , little tricky pay attention
Out[115]:
'ol'
In [40]:
len("hello")
Out[40]:
5
In [42]:
x = "hello"
len(x)
Out[42]:
5
In [46]:
"hello" == x
Out[46]:
True
In [49]:
'e' in x
Out[49]:
True
In [116]:
a = "there"
print("Hello"+a)
Hellothere
In [118]:
print("Hello"+" "+ a +"!")
Hello there!
In [119]:
type(a)
Out[119]:
str
In [120]:
input() #by defalut expects a string 
a
Out[120]:
'a'
In [121]:
x = input()
1
In [122]:
type(x)
Out[122]:
str
In [124]:
y = int(x)
type(y)
Out[124]:
int

Loops

In [128]:
n= int(input("enter a number ")) # be carefull about infinite loop

while n != 1:
    print("Entered number is not 1")
    n= int(input("enter a number"))
print ("Finally you entred 1!")
enter a number2
Entered number is not 1
enter a number3
Entered number is not 1
enter a number4
Entered number is not 1
enter a number5
Entered number is not 1
enter a number6
Entered number is not 1
enter a number345
Entered number is not 1
enter a number1
Finally you entred 1!
In [130]:
#while looop
n=0
while n<5:
    print(n)
    n= n+1
0
1
2
3
4
In [132]:
#for loop 
i= 0
for i in range(5):
    print(i)
0
1
2
3
4
In [135]:
i= 0
intsum = 0
for i in range(3,6): # only when i was equal to 3,4,5 this loop was executed hence intsum is 3+4+5
    intsum += i
print(intsum)
12
In [137]:
i= 0
intsum = 0
for i in range(3,6,2): # only when i was equal to 3,5 this loop was executed, last number represents "steps" jumops 2 steps
    intsum += i
print(intsum)
8
In [142]:
i=0
intsum = 0
for i in range(3,6,2): # only when i was equal to 3,5 this loop was executed, last number represents "steps" jumops 2 steps
    intsum += i
    if i == 3:
        break # lets you get out of the loops when you need
print(intsum)
3
In [151]:
for i in "hello": #loop through strings
    print(i)
h
e
l
l
o

Reference

  • edX course offered by MIT
  • 6.00.1x Introduction to Computer Science and Programming Using Python